package org.wgc.study.service;

import org.wgc.study.utils.fluentassembler.AsyncDataFetcher;
import org.wgc.study.utils.fluentassembler.DataKey;
import org.wgc.study.utils.fluentassembler.FluentAssembler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

/***
 *@title VideoAggregationService
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-08-01 9:23
 **/
public class VideoAggregationService {

    /*private final UserService userService;
    private final LikeService likeService;
    private final AttachmentService attachmentService;
    private final Executor executor;

    // constructor...

    public List<VideoVo> assembleVideoVos(List<Video> videos) {
        if (videos == null || videos.isEmpty()) {
            return new ArrayList<> ();
        }

        List<String> videoIds = videos.stream().map(Video::getId).collect(Collectors.toList());
        List<String> authorIds = videos.stream().map(Video::getAuthorId).distinct().collect(Collectors.toList());

        // --- 阶段一：定义数据准备任务 ---
        AsyncDataFetcher fetcher = new AsyncDataFetcher(executor);

        DataKey<Map<String, User>> authorMapKey = fetcher.register("authorMap",
                () -> userService.findUserMapByIds(authorIds));
        DataKey<Map<String, List<LikeUserDto>>> likeMapKey = fetcher.register("likeMap",
                () -> likeService.findLikeUsersMapByVideoIds(videoIds));
        DataKey<Map<String, Long>> attachmentCountKey = fetcher.register("attachmentCount",
                () -> attachmentService.countAttachmentsByVideoIds(videoIds));

        // --- 阶段二：执行并组装 ---
        return fetcher.executeAndAssemble(videos, (video, data) -> {
            // 在这个Lambda中，所有数据都已准备好，通过 key 安全获取
            Map<String, User> authorMap = data.get(authorMapKey);
            Map<String, List<LikeUserDto>> likeMap = data.get(likeMapKey);
            Map<String, Long> attachmentCountMap = data.get(attachmentCountKey);

            // 使用 FluentAssembler 进行组装
            return FluentAssembler.from(video, VideoVo::new)
                    .copyProperties()
                    .set(VideoVo::setAuthorName, () ->
                            Optional.ofNullable(authorMap.get(video.getAuthorId()))
                                    .map(User::getRealname)
                                    .orElse("Unknown Author")
                    )
                    .set(VideoVo::setLikeUsers, () ->
                            likeMap.getOrDefault(video.getId(), Collections.emptyList())
                    )
                    .set(VideoVo::setAttachmentCount, () ->
                            attachmentCountMap.getOrDefault(video.getId(), 0L)
                    )
                    .get();
        });
    }*/
}
